package com.cms.recursoshumanos.ui.user;

import com.cms.recursoshumanos.cache.UserInfoCache;
import com.cms.recursoshumanos.component.base.CustomButtonColored;
import com.cms.recursoshumanos.component.base.CustomEditFieldColored;
import com.cms.recursoshumanos.component.base.TwoColumnField;
import com.cms.recursoshumanos.constant.Constant;
import com.cms.recursoshumanos.container.ForegroundManager;
import com.cms.recursoshumanos.container.TwoColumnFieldManager;
import com.cms.recursoshumanos.handler.CambioUserInfoScreenHandler;
import com.cms.recursoshumanos.ui.RecursosHumanosScreen;

import net.rim.device.api.system.Bitmap;
import net.rim.device.api.system.CoverageInfo;
import net.rim.device.api.system.Display;
import net.rim.device.api.system.WLANInfo;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.FieldChangeListener;
import net.rim.device.api.ui.XYEdges;
import net.rim.device.api.ui.component.ButtonField;
import net.rim.device.api.ui.component.EditField;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.component.ObjectChoiceField;
import net.rim.device.api.ui.container.HorizontalFieldManager;
import net.rim.device.api.ui.decor.Border;
import net.rim.device.api.ui.decor.BorderFactory;

/**
 * A class extending the MainScreen class, which provides default standard
 * behavior for BlackBerry GUI applications.
 */
public final class ChangeUserInfoScreen extends RecursosHumanosScreen
{
    /**
     * Creates a new MyScreen object
     */
	//static Utileria tools = new Utileria();
	private CustomButtonColored boton_enviar_sol;
	private CustomEditFieldColored edit_info;
	private ObjectChoiceField selector_opcion;
	private HorizontalFieldManager hfm_boton_enviar;
	
	public static ChangeUserInfoScreen instance = null;
	
	public static ChangeUserInfoScreen getInstance()
	{
		if (instance == null)
		{
			instance = new ChangeUserInfoScreen();
		}
		return instance;
	}
	public ChangeUserInfoScreen() 
	{
		super(NO_VERTICAL_SCROLL | USE_ALL_HEIGHT );
		setComponents();
		
	}
	
	public boolean isConected()
	{
		 if(CoverageInfo.isOutOfCoverage())
         {
			 if(WLANInfo.getWLANState() == WLANInfo.WLAN_STATE_CONNECTED)
			 {
				 return true;
			 }
			 else 
			 {
				 return false;
			 }
         }
		 else
		 { 
			 return true; 
		 }             
	}
	public void setComponents() 
	{
		setTitle("Cambio de Informacion de Empleado");
		
		ForegroundManager foreground = new ForegroundManager();
        TwoColumnFieldManager twoColumnManager = new TwoColumnFieldManager( Display.getWidth() / 3, USE_ALL_WIDTH );
        twoColumnManager.setBorder( BorderFactory.createBitmapBorder( new XYEdges( 10, 10, 10, 10 ), Bitmap.getBitmapResource( "border_simple_white.png" ) ) );
        twoColumnManager.setMargin( 10, 10, 10, 10 );
        twoColumnManager.setPadding( 6, 6, 6, 6 );
        
        Border editBorder = BorderFactory.createBitmapBorder( new XYEdges( 10, 9, 9, 11 ), Bitmap.getBitmapResource( "border_edit.png" ) );
        
        selector_opcion = new ObjectChoiceField("Accion:",Constant.OPCION_INFO_CAMBIO,0,USE_ALL_WIDTH);
        twoColumnManager.add(selector_opcion);
        
        Field left;
        Field both;
        
        left = new LabelField( "Info de la Sol.:", FIELD_LEFT | FIELD_VCENTER );
        
        edit_info = new CustomEditFieldColored( FIELD_VCENTER|EditField.NO_NEWLINE, Constant.COLOR_EDIT_FOCUS);
        edit_info.setBorder( editBorder );
        edit_info.setMargin( 0, 0, 0, 6 );
        
        both = new TwoColumnField( left, edit_info, USE_ALL_WIDTH );
        both.setMargin( 6, 0, 6, 0 );
        twoColumnManager.add( both );
        
        hfm_boton_enviar = new HorizontalFieldManager(Field.FIELD_HCENTER); 
        boton_enviar_sol = new CustomButtonColored("Enviar Soliciutd",ButtonField.CONSUME_CLICK, Constant.COLOR_BUTTON_FOCUS); 
        boton_enviar_sol.setChangeListener(new FieldChangeListener() 
        {
	   		public void fieldChanged(Field field, int context) 
	       	{ 
	   			if (verificar_campos())
	   			{
	   				CambioUserInfoScreenHandler handler = new CambioUserInfoScreenHandler();
	   				handler.getEnvioCambioInfo(UserInfoCache.getInstance().getUsuario().getNombre_Usuario(), 
	   						UserInfoCache.getInstance().getUsuario().getPassword(), 
	   						selector_opcion.getChoice(selector_opcion.getSelectedIndex()).toString(), 
	   						edit_info.getText());
	   			}
	   			
	       	} 
		}); 
        
        hfm_boton_enviar.add(boton_enviar_sol);   
        twoColumnManager.add( hfm_boton_enviar );
        
        foreground.add( twoColumnManager );
        add( foreground );
        
        
			
	}
	public boolean verificar_campos()
	{
		if (edit_info.getText().length() > 0)
		{
			return true;
		}
		else
		{
			return false;
		}
	}
	
	
}
